package com.jywl.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.jywl.entity.EasyUiPageInfo;
import com.jywl.entity.User;
import com.jywl.mapper.UserMapper;
import com.jywl.util.CommUtil;
import com.jywl.util.ExcelUtil;
import com.jywl.util.FileUtil;
import com.jywl.util.ResUtil;


/** 
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2017-8-10 下午4:35:37 <br/>
 *
 * @author Sunxq
 * @version 
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserMapper userMapper;	
	@RequestMapping("/list")
	public String userList(String error,Model model) {
		return "user/user";
	}
	
	@RequestMapping("/query")  
	@ResponseBody
	
    public EasyUiPageInfo<User> queryUser(
    		@RequestParam("page") int page,
    		@RequestParam("rows") int rows,
    		@RequestParam HashMap<String, Object> params) {
	   PageHelper.startPage(page, rows);  
	   List<User> users = userMapper.query(params);
	   return new EasyUiPageInfo<User>(users);  
    } 
	
	@RequestMapping(value="/add",produces="application/json")  
	@ResponseBody	
    public HashMap<String, Object> addUser( @RequestBody List<User> users,HttpServletRequest request) {  
		HashMap<String, Object> res = ResUtil.getRes();
		for (User user : users) {
			if(null == user.getUserid()){
				user.setUserid(CommUtil.getUUID());
			}
		}
		userMapper.add(users);
       return res;  
    }
	@RequestMapping(value="/update",produces="application/json")  
	@ResponseBody	
    public HashMap<String, Object> editUser( @RequestBody List<User> users,HttpServletRequest request) {  
		HashMap<String, Object> res = ResUtil.getRes();
		
		userMapper.update(users);
       return res;  
    }
	@RequestMapping(value="/delete",produces="application/json")  
	@ResponseBody	
    public HashMap<String, Object> deleteUser( @RequestBody List<User> users,HttpServletRequest request) {  
		HashMap<String, Object> res = ResUtil.getRes();
		userMapper.delete(users);
       return res;  
    }
	
	@RequestMapping(value = "/exportByExcel") 
    public ModelAndView exportByExcel(@RequestParam HashMap<String, Object> params,HttpServletRequest request,  
            HttpServletResponse response) throws Exception {
		 
		PageHelper.startPage(0, 5000); 
		List<User> users = userMapper.query(params);
		ExcelUtil.exportExcelByModel("user.xlsx", users, request, response, "用户.xlsx");
        return null;  
    }
	
	/**获取Excel数据
	 * @param page
	 * @param rows
	 * @param params
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/importByExcel")  
	@ResponseBody
	public Map<String, Object> importByExcel(
			  @RequestParam("excelFile") CommonsMultipartFile excelFile,
			  @RequestParam(required = false) String filePre, HttpServletRequest request, HttpServletResponse response){ 
		List<User> users = null;
		HashMap<String, Object> res =  ResUtil.getRes();
		response.setContentType("application/json;charset=UTF-8"); 
		try {
			String path=FileUtil.upload(request);
			System.out.println(path);
			users= ExcelUtil.readExcelWithTitle(path, User.class);
			for (User user : users) {
				if(null == user.getUserid()){
					user.setUserid(CommUtil.getUUID());
				}
			}
			userMapper.add(users);
		} catch (Exception e) {
			res.put("resCode","2");
			res.put("messege",e.getMessage());
			e.printStackTrace();
		}
	return res;
	} 
}


